From 3925beaf37a268cb392eee9135b9f4ddc279b29b Mon Sep 17 00:00:00 2001 From: Iceyer Date: Thu, 3 Aug 2017 11:28:43 +0800 Subject: [PATCH] Add tests Change-Id: Ibcd2de8c0346bb0ad31555de392eaac7965bf3bc --- dtkcore.pro | 3 +- tests/dutiltester.cpp | 61 +++++++++++++++++++++++++++++++++++++++ tests/dutiltester.h | 18 ++++++++++++ tests/main.cpp | 5 ++++ tests/singletontester.cpp | 34 ++++++++++++++++++++++ tests/singletontester.h | 35 ++++++++++++++++++++++ tests/tests.pro | 21 ++++++++++++++ 7 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 tests/dutiltester.cpp create mode 100644 tests/dutiltester.h create mode 100644 tests/main.cpp create mode 100644 tests/singletontester.cpp create mode 100644 tests/singletontester.h create mode 100644 tests/tests.pro diff --git a/dtkcore.pro b/dtkcore.pro index 39f7fbf..1e7088f 100644 --- a/dtkcore.pro +++ b/dtkcore.pro @@ -4,4 +4,5 @@ CONFIG = ordered SUBDIRS += \ src \ - tool + tool \ + tests diff --git a/tests/dutiltester.cpp b/tests/dutiltester.cpp new file mode 100644 index 0000000..eb0df58 --- /dev/null +++ b/tests/dutiltester.cpp @@ -0,0 +1,61 @@ +#include "dutiltester.h" + +#include +#include +#include +#include + +#include "log/LogManager.h" +#include "filesystem/dpathbuf.h" +#include "singletontester.h" + +DCORE_USE_NAMESPACE + +TestDUtil::TestDUtil() +{ + +} + +void TestDUtil::testLogPath() +{ + qApp->setOrganizationName("deepin"); + qApp->setApplicationName("deepin-test-dtk"); + + DPathBuf logPath(QStandardPaths::standardLocations(QStandardPaths::HomeLocation).first()); + logPath = logPath / ".cache/deepin/deepin-test-dtk/deepin-test-dtk.log"; + + QCOMPARE(DLogManager::getlogFilePath(), logPath.toString()); +} + +void TestDUtil::testPathChange() +{ + DPathBuf root("/"); + + auto usr = root / "./usr"; + QCOMPARE(QDir(usr.toString()).absolutePath(), QDir::toNativeSeparators("/usr")); + + root /= "root"; + QCOMPARE(root.toString(), QDir::toNativeSeparators("/root")); + + root /= "../usr"; + QCOMPARE(root.toString(), usr.toString()); +} + +void TestDUtil::testDSingleton() +{ + auto threadA = new QThread; + auto testerA = new MultiSingletonTester; + connect(threadA, &QThread::started, testerA, &MultiSingletonTester::run); + testerA->moveToThread(threadA); + + auto threadB = new QThread; + auto testerB = new MultiSingletonTester; + testerB->moveToThread(threadB); + connect(threadB, &QThread::started, testerB, &MultiSingletonTester::run); + + threadA->start(); + threadB->start(); + + QThread::sleep(5); +} + diff --git a/tests/dutiltester.h b/tests/dutiltester.h new file mode 100644 index 0000000..c5e89a8 --- /dev/null +++ b/tests/dutiltester.h @@ -0,0 +1,18 @@ +#ifndef DUTILTESTER_H +#define DUTILTESTER_H + +#include + +class TestDUtil: public QObject +{ + Q_OBJECT +public: + TestDUtil(); + +private Q_SLOTS: + void testLogPath(); + void testPathChange(); + void testDSingleton(); +}; + +#endif // DUTILTESTER_H diff --git a/tests/main.cpp b/tests/main.cpp new file mode 100644 index 0000000..28005f9 --- /dev/null +++ b/tests/main.cpp @@ -0,0 +1,5 @@ +#include + +#include "dutiltester.h" + +QTEST_MAIN(TestDUtil); diff --git a/tests/singletontester.cpp b/tests/singletontester.cpp new file mode 100644 index 0000000..406aa82 --- /dev/null +++ b/tests/singletontester.cpp @@ -0,0 +1,34 @@ +/** + * Copyright (C) 2016 Deepin Technology Co., Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + **/ + +#include "singletontester.h" + +#include +#include + +Singleton::Singleton(QObject *parent) : QObject(parent) +{ + qDebug() << "Singleton Init Begin" << this; + QThread::sleep(3); + qDebug() << "Singleton Init End" << this; +} + +void Singleton::test() +{ + qDebug() << "test" << this; +} + +MultiSingletonTester::MultiSingletonTester(QObject *parent) : QObject(parent) +{ +} + +void MultiSingletonTester::run() +{ + Singleton::instance()->test(); +} diff --git a/tests/singletontester.h b/tests/singletontester.h new file mode 100644 index 0000000..ab8ba0d --- /dev/null +++ b/tests/singletontester.h @@ -0,0 +1,35 @@ +/** + * Copyright (C) 2016 Deepin Technology Co., Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + **/ + +#pragma once + +#include + +#include "base/dsingleton.h" + +class Singleton : public QObject, public Dtk::Core::DSingleton +{ + Q_OBJECT + friend class Dtk::Core::DSingleton; +public: + explicit Singleton(QObject *parent = 0); + + void test(); +}; + +class MultiSingletonTester : public QObject +{ + Q_OBJECT +public: + explicit MultiSingletonTester(QObject *parent = 0); + + void run(); +}; + + diff --git a/tests/tests.pro b/tests/tests.pro new file mode 100644 index 0000000..f26a5bd --- /dev/null +++ b/tests/tests.pro @@ -0,0 +1,21 @@ +TEMPLATE = app +QT += testlib +QT -= gui +CONFIG += testcase c++11 +TARGET = tests + +SOURCES += \ + main.cpp \ + dutiltester.cpp \ + singletontester.cpp + +HEADERS += \ + dutiltester.h \ + singletontester.h + +win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../src/release/ -ldtkcore +else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../src/debug/ -ldtkcore +else:unix: LIBS += -L$$OUT_PWD/../src/ -ldtkcore + +INCLUDEPATH += $$PWD/../src +DEPENDPATH += $$PWD/../src -- 2.30.2